import 'package:flutter/material.dart';
import 'package:qunaimedical/components/custom_divider.dart';
import 'package:qunaimedical/components/item_receiver.dart';
import 'package:qunaimedical/models/doctor.dart';
import 'package:qunaimedical/models/list_response.dart';
import 'package:qunaimedical/network/http.dart';
import 'package:qunaimedical/utils/toast_util.dart';
import 'package:qunaimedical/values/urls.dart';

/// 会诊接受人列表
class RecordReceiverListPage extends StatefulWidget {
  final String recordId;

  const RecordReceiverListPage({Key key, this.recordId}) : super(key: key);

  @override
  _RecordReceiverListPageState createState() => _RecordReceiverListPageState();
}

class _RecordReceiverListPageState extends State<RecordReceiverListPage> {
  List<Doctor> _item = [];

  @override
  void initState() {
    super.initState();
    _fetchData();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text('接受人')),
      body: ListView.separated(
          padding: EdgeInsets.symmetric(vertical: 16),
          itemBuilder: (c, i) => ItemReceiver(item: _item[i]),
          separatorBuilder: (c, i) => CustomDivider(),
          itemCount: _item.length),
    );
  }

  void _fetchData() async {
    var data = await XHttp.postJson(Urls.RECORD_RECEIVER_LIST, {
      "whetherDefault": "1",
      "id": widget.recordId,
    });
    if (data != null) {
      ListResponse response = ListResponse.fromJson(data);
      if (response.code == 1) {
        setState(() {
          _item = response.data.map((e) => Doctor.fromJson(e)).toList();
        });
      } else {
        ToastUtil.show(response.msg, context);
      }
    }
  }
}
